<?php

/**
 * Kesillo AutoLoader. This is pretty much the same as the PSR-0 Final Proposal.
 * https://gist.github.com/1234504
 * 
 * For the moment this works and we will keep it like this till we need further
 * functionality.
 * 
 * NOTE: This method needs improvement and more error checking. For now we leave
 * it as is because as long as we follow the apps rules, we wont have issues
 * loading or finding the classes.
 * 
 * @param string $className The full name of the class 
 */

namespace Kesillo\Core;

function LoadClass( $className )
{
    $className = ltrim($className, '\\');
    $fileName  = '';
    $namespace = '';
    
    if ($lastNsPos = strripos($className, '\\')) 
    {
        $namespace = substr($className, 0, $lastNsPos);
        $className = substr($className, $lastNsPos + 1);
        $fileName  = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
    }
    
    $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';

    if (is_file(LIBPATH.$fileName))
        require LIBPATH.$fileName;
    else
        require APPSPATH.$fileName;
    
}


spl_autoload_register( 'Kesillo\Core\LoadClass' );